<!-- 
	Transactions page
	
	@author .mrduongnv@gmail.com  
-->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<%@include file="/WEB-INF/taglibs/common/taglibs.jsp"%>


<head>
<title><spring:message code="sp.newtransaction.title" /></title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<meta http-equiv="Expires" content="0" />
<meta http-equiv="Cache-Control" content="no-cache" />
<meta http-equiv="Pragma" content="no-cache" />
<link rel="stylesheet" href="<c:url value="/css/styles.css"/>" />
<script src="<c:url value="/javascripts/common.js"/>" type="text/javascript"></script>

<link rel="stylesheet"	href="<c:url value="/javascripts/jscalendar/theme/calendar-system.css"/>" />
<script src="<c:url value="/javascripts/jscalendar/calendar-cfg.js"/>" type="text/javascript"></script>
<script src="<c:url value="/javascripts/calendar.js"/>" type="text/javascript"></script>
<script	src="<c:url value="/javascripts/jscalendar/lang/calendar-en.js"/>" type="text/javascript"></script>
<script src="<c:url value="/javascripts/jscalendar/calendar-setup.js"/>" type="text/javascript"></script>
<script src="<c:url value="/javascripts/jquery-1.4.4.js"/>" type="text/javascript"></script>

<script language="javascript" type="text/javascript">

function isInt(textBox){
	return /^\d+$/.test(textBox.value);
}
 
function restoreNormalFieldView(){
	try{
		var elems=$(".selfFee");
        var i;
		 for(i=0;i<elems.length;i++){
		    elems[i].style.borderColor="#000000";
		}
		 
		elems = $(".taxPrepFee");
		for (i = 0; i < elems.length; i++) {
		    elems[i].style.borderColor = "#000000";
		}
		
		elems = $(".electronicPrepFee");
		for (i = 0; i < elems.length; i++) {
		    elems[i].style.borderColor = "#000000";
		}
		
		elems = $(".electronicFillingFee");
		for (i = 0; i < elems.length; i++) {
		    elems[i].style.borderColor = "#000000";
		}
		
		elems = $(".serviceBureauFee");
		for (i = 0; i < elems.length; i++) {
		    elems[i].style.borderColor = "#000000";
		}
		
		elems = $(".fedBankProductFee");
		for (i = 0; i < elems.length; i++) {
		    elems[i].parentNode.style.borderStyle = "none";
		}

		elems = $(".stateProductFee");
		for (i = 0; i < elems.length; i++) {
		    elems[i].parentNode.style.borderStyle = "none";
		}
		
		
		elems = $(".customLabelValue1");
		for (i = 0; i < elems.length; i++) {
		    elems[i].style.borderColor = "#000000";
		}
		
		elems = $(".customLabelValue2");
		for (i = 0; i < elems.length; i++) {
		    elems[i].style.borderColor = "#000000";
		}

		elems = $(".payeeSelector");
		for (i = 0; i < elems.length; i++) {
		    elems[i].parentNode.style.borderStyle = "none";
		}
		
		elems = $(".payeeAmount");
		for (i = 0; i < elems.length; i++) {
		    elems[i].style.borderColor = "#000000";
		}
		
		setErrorMessage("");
     }catch(err){}
}

 function approveTransaction(){
     restoreNormalFieldView();
     var validationResult=true;
     var i=0;
     var elems=$(".selfFee");
     for(i=0;i<elems.length;i++){
         validationResult=isInt(elems[i]);
         if(!validationResult){
        	 setErrorMessage("Entered value should be a positive integer number");
             elems[i].style.borderColor="#ff0000";
             elems[i].focus();
             return;
         }
     }
      elems = $(".taxPrepFee");
     for (i = 0; i < elems.length; i++) {
         validationResult = isInt(elems[i]);
         if (!validationResult) {
        	 setErrorMessage("Entered value should be a positive integer number");
             elems[i].style.borderColor = "#ff0000";
             return;
         }
     }
     elems = $(".electronicPrepFee");
     for (i = 0; i < elems.length; i++) {
         validationResult = isInt(elems[i]);
         if (!validationResult) {
        	 setErrorMessage("Entered value should be a positive integer number");
             elems[i].style.borderColor = "#ff0000";
             return;
         }
     }
     elems = $(".electronicFillingFee");
     for (i = 0; i < elems.length; i++) {
         validationResult = isInt(elems[i]);
         if (!validationResult) {
        	 setErrorMessage("Entered value should be a positive integer number");
             elems[i].style.borderColor = "#ff0000";
             return;
         }
     }
     elems = $(".serviceBureauFee");
     for (i = 0; i < elems.length; i++) {
         validationResult = isInt(elems[i]);
         if (!validationResult) {
        	 setErrorMessage("Entered value should be a positive integer number");
             elems[i].style.borderColor = "#ff0000";
             return;
         }
     }
 
     elems = $(".customLabelValue1");
     for (i = 0; i < elems.length; i++) {
         validationResult = isInt(elems[i]);
         if (!validationResult) {
        	 setErrorMessage("Entered value should be a positive integer number");
             elems[i].style.borderColor = "#ff0000";
             return;
         }
     }
     elems = $(".customLabelValue2");
     for (i = 0; i < elems.length; i++) {
         validationResult = isInt(elems[i]);
         if (!validationResult) {
        	 setErrorMessage("Entered value should be a positive integer number");
             elems[i].style.borderColor = "#ff0000";
             return;
         }
     }
     
     elems=$(".payeeAmountRow");
     for(i = 0; i < elems.length; i++) {
    	 if ($(elems[i]).is(":visible")) {
    		 var payeeSelect = $(elems[i]).find("select");
    		 if (payeeSelect[0].value == 0) {
    			 setErrorMessage("Please select a payee");
                 payeeSelect[0].parentNode.style.borderStyle = "solid";
                 return;
    		 }
    	 }
     }
     
     elems = $(".payeeAmount");
     for (i = 0; i < elems.length; i++) {
    	 if ($(elems[i]).is(":visible")) {
	         validationResult = isNumeric(elems[i].value) && elems[i].value > 0;
	         if (!validationResult) {
	        	 setErrorMessage("Entered value should be a positive number");
	             elems[i].style.borderColor = "#ff0000";
	             return;
	         }
    	 }
     }
     
     elems=$(".isSelected");
     validationResult=false;
     for(i=0;i<elems.length;i++){
         if(elems[i].checked){
             validationResult=true;
             break;
         }
     }
     if(!validationResult){
    	setErrorMessage("There are no transactions were selected");
        return;
     }
     
     if(validationResult){
 		ajaxFormSubmit("approve_transaction.do");
     }
 }
 
 function setErrorMessage(message) {
		$("#error").replaceWith("<div id=\"error\" class=\"errors\">" + message + "</div>"); 
 }
 
 function backToTransactionApproval() {
	 ajaxFormSubmit("back_to_approval.do");
 }
 
 function ajaxFormSubmit(actionUrl) {
     $.ajax({
     	type: "POST",
     	url: actionUrl,
     	data: $("#main").serialize(),
     	success: function(response) {
     		$("#transactionFormContents").replaceWith("<div id=\"transactionFormContents\">" + response + "</div>");
     	} 
     });
 }
 
 function confirmTransactions() {
	 $("#main").submit();
 }
 
function addPayee(transactionIndex) {
	var requestData = getRequestData(transactionIndex);
	
	$.ajax({
		type: "POST",
		url: "add_payee.do",
		data: requestData,
		success: function(response) {
			replacePayeeTable(response, transactionIndex);			
		}
	});
 }
 
 function deletePayee(transactionIndex, payeeIndex) {
	 	var requestData = getRequestData(transactionIndex);
	 	requestData = requestData + "&payeeIndex=" + payeeIndex;
	 
		$.ajax({
			type: "POST",
			url: "delete_payee.do",
			data: requestData,
			success: function(response) {
				replacePayeeTable(response, transactionIndex);
			}
		});	 
 }
 
 function getRequestData(index) {
		var requestData = "index=" + index;
		var payeeSelects = $("#payeeTable" + index).find("select");
        var i = 0;
		for (i = 0; i < payeeSelects.length; i++) {
			requestData = requestData + "&payeeId_" + index + "_" + i + "=" + payeeSelects[i].options[payeeSelects[i].selectedIndex].value; 
		}
		requestData = requestData + "&idCount=" + payeeSelects.length;
		
		var payeeAmounts = $("#payeeTable" + index).find("input:text");
		for (i = 0; i < payeeAmounts.length; i++) {
			requestData = requestData + "&payeeAmount_" + index + "_" + i + "=" + payeeAmounts[i].value; 
		}
		requestData = requestData + "&amountCount=" + payeeAmounts.length;

		return requestData;
 }
 
 function replacePayeeTable(newTableHtml, tableIndex) {
 	newTableHtml = newTableHtml.replace(/<form.+?>/, "");
 	newTableHtml = newTableHtml.replace(/<\/[ ]*form>/, "");
 	$("#payeesList" + tableIndex).replaceWith("<div id=\"payeesList" + tableIndex + "\">" + newTableHtml + "</div>");
	 
 }
 
 function enableLabels(labelName, labelClass) {
	 var disabled = true;
	 if ($("#" + labelName).val().length > 0 ) {
		 disabled = false;
	 }
	 
	 var labels = $("." + labelClass);
	 for (var i = 0; i < labels.length; i++) {
		labels[i].disabled = disabled;
	 }
 }
 
 $(document).ready(function() {
	enableLabels("customLabelName1", "customLabelValue1"); 
	enableLabels("customLabelName2", "customLabelValue2");	
 });
</script>

</head>

<body>
<div class="page"><%@include file="../top_menu.jsp"%><table
	width=100%>
	<tr>
		<td><%@include file="/WEB-INF/jsp/include/messages.jsp"%>
		<br />
		</td>
	</tr>

</table>

<div class="contentArea">

	<table border=0 cellpadding=0 cellspacing=0>
		<%--@elvariable id="hasPayees" type="java.lang.Boolean"--%>
		<c:if test="${hasPayees == false}">
			<tr class="error">
				<td><spring:message code="sp.newtransaction.label.nopayeefoundmessage" /> <a
					href="<c:url value="/users/create_user.htm" />"><spring:message code="sp.newtransaction.label.here" /></a>
				<spring:message code="sp.newtransaction.label.tocreatepayee" />.</td>
			</tr>
		</c:if>
	</table>

	<div id="transactionFormContents">	
		<%@include file="/WEB-INF/jsp/serviceprovider/transaction/transaction_form.jsp"%>
	</div>

<div id="error" class="errors"></div>

<script language="javascript" type="text/javascript">ch_select(true);</script>
</div>
</div><%@include file="/WEB-INF/jsp/include/footer.jsp"%>
</body>
</html>